package com.mayikt.days07;

/**
 * @ClassName Test004
 * @Author 蚂蚁课堂余胜军 QQ644064779 www.mayikt.com
 * @Version V1.0
 **/
public class Test004 {
    private String context;

    public String getContext() {
        return threadLocal.get();
//        return context;
    }

    public void setContext(String context) {

//        this.context = context;
        threadLocal.set(context);
    }

    private static Object lock = new Object();
    private static ThreadLocal<String> threadLocal = new ThreadLocal<>();

    public static void main(String[] args) {
        Test004 test004 = new Test004();
        for (int i = 0; i < 5; i++) {
            int finalI = i;
            new Thread(() -> {
//                synchronized (lock) {
                test004.setContext(Thread.currentThread().getName() + "," + finalI);
                try {
                    Thread.sleep(500);
                } catch (Exception e) {

                }
                System.out.println("线程id:"
                        + Thread.currentThread().getName() + "," + test004.getContext());
//                }


            }, i + "").start();
        }
    }
}
